#include "sys_stdio.h"
#include "task_manager.h"
#include "control_task.h"
#include "gui_object.h"
#include "gui_entry.h"
#include "sys_operater.h"
#include "lcd.h"
#include "event_def.h"
#include "gui_task.h"
#include "main_task.h"

struct gs_control_task
{
    struct sj_task_t control_task_handle;
    int type_input;
    struct control_init_param param;
    int fd_uart3;
};
static struct gs_control_task gs_control_task_all = {0};

GUI_CONSOLE_INIT(gs_console);

static TASK_MODULE_DATA(control,CONTROL_TASK_PRIO,CONTROL_STK_SIZE);

void control_task_init(struct control_init_param *param)
{
    gs_control_task_all.fd_uart3=sj_open("/dev/uart3",0);

    //sj_printf("fd_uart3 %d \r\n",gs_control_task_all.fd_uart3);

    gs_control_task_all.param = *param;
    gui_entry_get(&gs_console);//to desktop slj add
    gs_control_task_all.control_task_handle.name="control";
    gs_control_task_all.control_task_handle.run_func = control_task;
    GET_TASK_MODULE_DATA(gs_control_task_all.control_task_handle.task_data,control);
    gs_control_task_all.control_task_handle.task_data->periodtime=500;
    sj_task_create(&gs_control_task_all.control_task_handle);
}

void control_task_input_type(sj_int8 type)
{
    gs_control_task_all.type_input=type;
    //sj_printf("enter control_task_input_type %d  \r\n",gs_control_task_all.type_input);
    sj_task_run_onetime(&gs_control_task_all.control_task_handle);
}

void control_task(void *p_arg)
{
    int ret =0;
    {
       sj_int8 value=0;
       if(sj_task_get_current_state(&gs_control_task_all.control_task_handle)==TASK_STATUS_EVENT)
       {
            value=gs_control_task_all.type_input;
       }

       if(value ==0)
       {

       }
       else if(value == KEY_DOWN_VALUE)
       {
            gui_object_go_peer_next(&gs_console);
            gs_control_task_all.param.gui_run(&gs_console);
       }
       else if(value == KEY_UP_VALUE)
       {
            gui_object_go_peer_priv(&gs_console);
            gs_control_task_all.param.gui_run(&gs_console);
       }
       else if(value == KEY_OK_VALUE)
       {
            //sj_write(gs_control_task_all.fd_uart3,"uart3\r\n",strlen("uart3\r\n"));

            gui_object_into_child(&gs_console);
            gs_control_task_all.param.gui_run(&gs_console);
       }
       else if(value == KEY_MENU_VALUE)
       {
            gui_object_go_back_to_parent(&gs_console);
            gs_control_task_all.param.gui_run(&gs_console);
       }
       else if(value>KEY_LONG_VALUE_OFFSET)
       {

       }
    }
}
